﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Outlook.WorkItems.Extension.Manager.TeamProjectManager;
using Outlook.WorkItems.Extension.Controls;

namespace Outlook.WorkItems.Extension.Addin.Forms
{
    public partial class CreateSubFoldersForm : Form
    {
        private readonly TeamProjectLink _link;
        private readonly MAPIFolder _folder;

        public CreateSubFoldersForm()
        {
            InitializeComponent();
        }

        public CreateSubFoldersForm(MAPIFolder folder)
            : this()
        {
            _folder = folder;
            _link = TeamProjectLinksManager.GetTeamProjectLink(folder.EntryID);
            WorkItemStore store = TeamProjectLinksManager.GetWorkItemStore(_link.TeamServerUri);
            treeView1.CheckBoxes = true;
            foreach (WorkItemType wit in store.Projects[_link.TeamProjectName].WorkItemTypes)
            {
                TreeNode node = treeView1.Nodes.Add(wit.Name);
                node.Tag = wit;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in treeView1.Nodes)
            {
                if (node.Checked)
                {
                    MAPIFolder f = _folder.Folders.Add(node.Text, OlDefaultFolders.olFolderInbox);

                    CreateWiAction action = null;
                    if (_link.CreateWiAction != null)
                    {
                        action = new CreateWiAction()
                                     {
                                         AddMessageAttachments = _link.CreateWiAction.AddMessageAttachments,
                                         AddMessageToAttachmentWi = _link.CreateWiAction.AddMessageToAttachmentWi,
                                         FormatMail = _link.CreateWiAction.FormatMail,
                                         TrackConversationInWorkItem = _link.CreateWiAction.TrackConversationInWorkItem,
                                         Mapping = new WorkItemMapping()
                                                 {
                                                     WorkItemType = node.Text,
                                                     MappingFields = new List<MappingField>()
                                                 }
                                     };
                    }
                    TeamProjectLinksManager.AddTeamProjectLink(f.EntryID, f.StoreID, _link.TeamServerName, _link.TeamServerUri, _link.TeamProjectName, action, null);
                }
            }
            foreach (TreeNode node in teamTreeViewUC1.treeView1.Nodes.FindNodes(t => t.Checked))
            {
                string path = "";
                if (node is DocumentLibrairyNode)
                {
                    path = node.Tag.ToString();
                    path = path.Substring(0, path.LastIndexOf("/Forms"));
                }
                else if (node is FolderNode)
                {
                    var fNode = node as FolderNode;
                    string doc = fNode.DocumentLibrairyNode.Tag.ToString();
                    doc = doc.Substring(0, doc.LastIndexOf("/Forms"));
                    string folder = node.Tag.ToString();
                    path = doc + folder.Substring(folder.IndexOf("/"), folder.Length - folder.IndexOf("/"));
                }
                var f = _folder.Folders.Add(node.Text, OlDefaultFolders.olFolderInbox);
                var action = new UploadAttachmentsAction() { DestinationPath = path };
                TeamProjectLinksManager.AddTeamProjectLink(f.EntryID, f.StoreID, _link.TeamServerName, _link.TeamServerUri, _link.TeamProjectName, null, action);
            }
            Close();
        }

        private void CreateSubFoldersForm_Load(object sender, EventArgs e)
        {
            teamTreeViewUC1.InitTreeView();
        }
    }
}
